package com.mall.order.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.converter.OrderConverter;
import com.mall.order.dal.entitys.*;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.OrderShippingMapper;
import com.mall.order.dto.*;
import com.mall.order.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 *  ciggar
 * create-date: 2019/7/30-上午10:04
 */
@Slf4j
@Service
public class OrderQueryServiceImpl implements OrderQueryService {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderConverter orderConverter;
    @Autowired
    OrderItemMapper orderItemMapper;
    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Override
    public OrderListResponse queryAllOrderList(OrderListRequest orderListRequest) {
        Integer page = orderListRequest.getPage();
        Integer size = orderListRequest.getSize();
        String sort = orderListRequest.getSort();
        Long userId = orderListRequest.getUserId();

        OrderListResponse orderListResponse = new OrderListResponse();

        //分页
        PageHelper.startPage(page, size);

        Example example = new Example(Order.class);
        example.createCriteria()
                .andEqualTo("userId", userId);
        //查询order表
        List<Order> orders = orderMapper.selectByExample(example);
        List<OrderDetailInfo> arrayList = new ArrayList<>();
        for (Order order : orders) {

            //封装order表  类型转换
            OrderDetailInfo orderDetailInfo = orderConverter.order2detail(order);

            //查询orderItem表
            Example example1 = new Example(OrderItem.class);
            example1.createCriteria().andEqualTo("orderId", order.getOrderId());
            List<OrderItem> orderItems = orderItemMapper.selectByExample(example1);
            //封装orderItem表  类型转换
            List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);
            orderDetailInfo.setOrderItemDto(orderItemDtos);

            //查询orderShipping表
            OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(order.getOrderId());
            //封装orderShipping 类型转换
            OrderShippingDto orderShippingDto = orderConverter.shipping2dto(orderShipping);
            orderDetailInfo.setOrderShippingDto(orderShippingDto);

            //将封装好的数据放入list
            arrayList.add(orderDetailInfo);

        }

        //获得总页数total
        PageInfo pageInfo = new PageInfo(orders);
        long total = pageInfo.getTotal();

        //封装orderListResponse
        orderListResponse.setData(arrayList);
        orderListResponse.setTotal(total);

        return orderListResponse;

    }

    @Override
    public OrderDetailResponsee queryOrderDetail(OrderDetailRequest orderDetailRequest) {
        OrderDetailResponsee orderDetailResponsee = new OrderDetailResponsee();
        //查询order表
        Order orderSelect = new Order();
        orderSelect.setOrderId(orderDetailRequest.getOrderId());
        Order order = orderMapper.selectByPrimaryKey(orderSelect);
        //查询orderItem表
        OrderItem orderItemSelect = new OrderItem();
        orderItemSelect.setOrderId(orderDetailRequest.getOrderId());
        List<OrderItem> orderItems = orderItemMapper.select(orderItemSelect);
        Double sum = 0.0;
        for (OrderItem orderItem : orderItems) {
            sum += orderItem.getTotalFee();
        }
        //查询orderShipping表
        OrderShipping orderShippingSelect = new OrderShipping();
        orderShippingSelect.setOrderId(orderDetailRequest.getOrderId());
        OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(orderShippingSelect);

        //封装外层数据
        orderDetailResponsee.setUserName(order.getBuyerNick());
        orderDetailResponsee.setOrderTotal(sum);
        orderDetailResponsee.setOrderStatus(order.getStatus());
        orderDetailResponsee.setTel(orderShipping.getReceiverPhone());
        orderDetailResponsee.setStreetName(orderShipping.getReceiverAddress());
        //封装goodsList
        List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);
        orderDetailResponsee.setGoodsList(orderItemDtos);

        return orderDetailResponsee;

    }

}
